package com.vanvalt.payhui.modules.pay.api;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.core.Context;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.vanvalt.payhui.modules.pay.entity.Profit;
import com.vanvalt.payhui.modules.pay.service.BankCardService;
import com.vanvalt.payhui.modules.pay.service.BannerService;
import com.vanvalt.payhui.modules.pay.service.DownloadService;
import com.vanvalt.payhui.modules.pay.service.FeedbackService;
import com.vanvalt.payhui.modules.pay.service.NewsService;
import com.vanvalt.payhui.modules.pay.service.PosService;
import com.vanvalt.payhui.modules.pay.service.ProfitDataService;
import com.vanvalt.payhui.modules.pay.service.ProfitService;
import com.vanvalt.payhui.modules.pay.service.QuestionService;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.modules.pay.service.UserShareService;

@Controller
@Scope("prototype")
@RequestMapping(value = "/api")
public class Service {
	/**
	 * 日志.
	 */
	private static Logger					logger				= Logger.getLogger(Service.class);
	private HttpServletRequest 				request;
    private HttpServletResponse 			response;
	private static final String				CONTENT_TYPE		= "Content-Type";
	private static final String				APPLICATION_CHARSET	= "application/json; charset=utf-8";
	private static final int				STATUS_200			= 200;
	private static final String				CHARSET				= "utf-8";
	
	/**
	 * 输出。
	 */
	private String							output;
	
	@Autowired
	private SystemService					systemService;
	
	@Autowired
	private OfficeService					officeService;
	
	@Autowired
	private UserService						userService;
	
	@Autowired
	private BannerService 					bannerService;
	
	@Autowired
	private PosService						posService;
	
	@Autowired
	private DownloadService					downloadService;
	
	@Autowired
	private QuestionService					questionService;
	
	@Autowired
	private NewsService						newsService;
	
	@Autowired
	private UserShareService				userShareService;
	
	@Autowired
	private ProfitDataService				profitDataService;
	
	@Autowired
	private FeedbackService					feedbackService;
	
	@Autowired
	private ProfitService					profitService;
	
	@Autowired
	private BankCardService					bankCardService;
	
	@POST
	//@Path("json")
	@ResponseBody
	@RequestMapping(value = "json", produces="application/json")
	public void request(@Context HttpServletRequest request, @Context HttpServletResponse response) throws IOException {
		// 初始化
		RequestProcess requestProcess = new RequestProcess(request);

		if (null == output) {
			// 设置服务
			setService(requestProcess);
			// 执行处理
			requestProcess.ProcessCommand();
			// 输出json
			output = requestProcess.getResponseCommand().toJson();
		}

		// 输出到客户端
		//return Response.status(STATUS_200).header(CONTENT_TYPE, APPLICATION_CHARSET).entity(output).build();
		// 输出到客户端
		response.setContentType(CONTENT_TYPE);
		response.setCharacterEncoding(CHARSET);
		PrintWriter out = response.getWriter();
		out.write(output);
		out.flush();
		out.close();
	}

	/**
	 * 设置服务
	 * 
	 * @param teacherRequestProcess
	 */
	private void setService(RequestProcess requestProcess) {
		// 设置服务接口
		requestProcess.setSystemService(systemService);
		requestProcess.setOfficeService(officeService);
		requestProcess.setUserService(userService);
		requestProcess.setBannerService(bannerService);
		requestProcess.setPosService(posService);
		requestProcess.setDownloadService(downloadService);
		requestProcess.setQuestionService(questionService);
		requestProcess.setNewsService(newsService);
		requestProcess.setUserShareService(userShareService);
		requestProcess.setProfitDataService(profitDataService);
		requestProcess.setFeedbackService(feedbackService);
		requestProcess.setBankCardService(bankCardService);
		requestProcess.setProfitService(profitService);
	}
	
	public String getOutput() {
		return output;
	}

	public void setOutput(String output) {
		this.output = output;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}
	
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	

}